﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _2011DBProject
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Settings.RefreshSessionVariables(true, false);
            lblUserID.Text = Settings.strAccountID;

            if ((string)(Session["USER"]) == null)
            {
                Response.Redirect("default.aspx?isNoSession=1");
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void btnGoLogout_onclick(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        protected void btnGoServer_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Server.aspx");
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList2.DataBind();
        }
        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSendItem.Visible = true;
            lblMsg1.Visible = true;
            lblMsg2.Visible = true;
            lblMsg3.Visible = true;
            DropDownList1.Visible = true;
            DropDownList2.Visible = true;
            lblHid.Visible = false;
        }

        protected void btnSendItem_Click(object sender, EventArgs e)
        {
            try
            {
                //넣어주기
                DataSet1.ITEMDataTable dtSelectedItem = new DataSet1.ITEMDataTable();
                DataSet1TableAdapters.ITEMTableAdapter taItem = new DataSet1TableAdapters.ITEMTableAdapter();
                taItem.Connection.ConnectionString = Settings.strConnectionString;
                taItem.FillITEM(dtSelectedItem, (decimal)GridView2.SelectedDataKey[0]);

                DataSet1.MY_ITEMDataTable dtMyItem = new DataSet1.MY_ITEMDataTable();
                DataSet1.MY_ITEMRow myR = dtMyItem.NewMY_ITEMRow();
                DataSet1TableAdapters.MY_ITEMTableAdapter taMyItem = new DataSet1TableAdapters.MY_ITEMTableAdapter();
                taMyItem.Connection.ConnectionString = Settings.strConnectionString;

                myR.CHARACTER_NAME = DropDownList2.SelectedValue;
                myR.SERVER_NO = Convert.ToDecimal(DropDownList1.SelectedValue);
                myR.ITEM_NO = Convert.ToDecimal(GridView2.SelectedDataKey[0]);
                if (!dtSelectedItem[0].IsMAX_DURABILITYNull())
                    myR.DURABILITY = dtSelectedItem[0].MAX_DURABILITY;
                if(!dtSelectedItem[0].IsPOWER_CORRECTIONNull())
                    myR.POWER_CORRECTION = dtSelectedItem[0].POWER_CORRECTION;

                if (!dtSelectedItem[0].IsIMPACT_CORRECTIONNull())
                    myR.IMPACT_CORRECTION = dtSelectedItem[0].IMPACT_CORRECTION;

                if (!dtSelectedItem[0].IsTECHNIQUE_CORRECTIONNull())
                    myR.TECHNIQUE_CORRECTION = dtSelectedItem[0].TECHNIQUE_CORRECTION;

                if (!dtSelectedItem[0].IsSTRENGTH_CORRECTIONNull())
                    myR.STRENGTH_CORRECTION = dtSelectedItem[0].STRENGTH_CORRECTION;

                myR.CADDIE_EQUIP = "N";
                myR.CHARACTER_EQUIP = "N";
                myR.LOCKERROOM_KEEP = "N";
                myR.QTY = (decimal)GridView2.SelectedDataKey[1];
                myR.INVEN_ITEM_UNIQ_NO = getNextInven_Item_Uniq_No(myR.ITEM_NO);

                dtMyItem.AddMY_ITEMRow(myR);
                taMyItem.Update(dtMyItem);

                //가상락커룸에 아이템 삭제하기
                DataSet1TableAdapters.VIRTUAL_LOCKERROOMTableAdapter taVir = new DataSet1TableAdapters.VIRTUAL_LOCKERROOMTableAdapter();
                taVir.Connection.ConnectionString = Settings.strConnectionString;
                DataSet1.VIRTUAL_LOCKERROOMDataTable dtVir = new DataSet1.VIRTUAL_LOCKERROOMDataTable();
                taVir.FillByItemNoAccountID(dtVir, (decimal)GridView2.SelectedDataKey[0], (string)Session["USER"]);
                DataSet1.VIRTUAL_LOCKERROOMRow vr = dtVir[0];
                vr.Delete();
                taVir.Update(dtVir);
                GridView2.DataBind();
                

                //버튼 가리고, 완료 메시지 보이고
                btnSendItem.Visible = false;
                lblMsg1.Visible = false;
                lblMsg2.Visible = false;
                lblMsg3.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                lblHid.Visible = true;
                
            }
            catch
            {
               string script = "<script>alert('에러번호 - #07 : 관리자에게 문의하세요.');</script>";
               Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
            }
        }

        private decimal getNextInven_Item_Uniq_No(decimal itemno)
        {
            decimal r = 1;
            try
            {
                System.Data.OracleClient.OracleConnection cnn = new System.Data.OracleClient.OracleConnection(Settings.strConnectionString);
                System.Data.OracleClient.OracleCommand command = new System.Data.OracleClient.OracleCommand("select count(*) from MY_ITEM where CHARACTER_NAME = '" + DropDownList2.SelectedValue + "' and SERVER_NO = " + DropDownList1.SelectedValue + " and ITEM_NO = '" + itemno + "'", cnn);
                cnn.Open();
                r = Convert.ToDecimal(command.ExecuteScalar());
                cnn.Close();
                return r+1;
            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}

